<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Variables in Ruby</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Ruby, language, tutorial, variables, learn Ruby">
<meta name="description" content="In this part of the Ruby tutorial, we cover Ruby variables.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content2">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Variables in Ruby</h1>

<p>
In this part of the Ruby tutorial, we will examine variables 
in more detail.
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>

<p>
A <b>variable</b> is a place to store data. Each variable is
given a unique name. There are some naming conventions which apply
to variable names. Variables hold objects. More precisely, they
refer to a specific object located in computer memory. Each
object is of certain data type. There are built-in data types
and there are custom-built data types. Ruby belongs to the family
of dynamic languages. Unlike strongly typed languages like
Java, C or Pascal, dynamic languages do not declare a variable
to be of certain data type. Instead of that, the interpreter 
determines the data type at the moment of the assignment. 
Variables in Ruby can contain different values and different types
of values over time.
</p>


<pre class="code">
#!/usr/bin/ruby

i = 5
p i
i = 7
p i
</pre>

<p> 
The term variable comes from the fact, that variables can take 
different values over time. Unlike constants. In the example above
there is a variable called i. First it is assigned a value 5, later
a different value 7. The values of a variable can vary over time. 
</p>

<h2>Naming conventions</h2>

<p>
Ruby, like any other programming language, has some naming conventions
for variable indentifiers. 
</p>

<p>
Ruby is a <b>case sensitive</b> language. It means that age and Age are
two different variable names. Most languages are case sensitive. 
BASIC is an exception, it is a case insensitive language. While we
can create different names by changing the case of the characters,
this practice is not recommended. 
</p>

<pre class="code">
#!/usr/bin/ruby

i = 5
p i
I = 7
p I
</pre>

<p>
The code example defines two variables. I and i. They hold different
values. 
</p>

<pre>
./case.rb
5
7
</pre>

<p>
Output of the case.rb example. 
</p>

<hr class="btm">

<p>
Variable names in Ruby can be created from alfanumeric characters 
and underscore (_) character. A variable cannot begin with a number. 
The interpreter can then easier distinguish between a literal number and a variable.
Variable names cannot begin with a capital letter. If an identifier begins
with a capital letter, it is considered to be a constant in Ruby.
</p>

<pre class="code">
#!/usr/bin/ruby

name = "Jane"
placeOfBirth = "Bratislava"
placeOfBirth = "Kosice"
favorite_season = "autumn"

n1 = 2
n2 = 4
n3 = 7

p name, placeOfBirth, favorite_season
p n1, n2, n3
</pre>

<p>
In this script, we show a few valid variable names.
</p>

<hr class="btm">

<p>
Variable names should be <b>meaningful</b>. It is a good programming practice
to choose descriptive names for variables. The programs are more readable
then. 
</p>

<pre class="code">
#!/usr/bin/ruby

name = "Jane"
place_of_birth = "Bratislava"
occupation = "student"

i = 5
while i > 0 do
    puts name
    i -= 1
end
</pre>

<p>
The script shows three descriptive variable names. The
place_of_birth is more descriptive to a programmer than
e.g. pob. It is generally considered OK to choose simple variable names
in loops. 
</p>

<h2>Sigils</h2>

<p>
Variable identifiers can start with special characters also called <b>sigils</b>.
A sigil is a symbol attached to an identifier.
Variable sigils in Ruby denote variable scope. In contrast to Perl, where sigils denote
data type. The Ruby variable sigils are $ and @.
</p>

<pre class="code">
#!/usr/bin/ruby

tree_name = "pine"
$car_name = "Peugeot"
@sea_name = "Black sea"
@@species = "Cat"

p local_variables
p global_variables.include? :$car_name
p self.instance_variables
p Object.class_variables
</pre>

<p>
We have four variables with different scopes. A scope is the range in which 
a variable can be referenced. We use special built-in
methods to determine the scope of the variables. 
</p>

<pre class="explanation">
tree_name = "pine"
</pre>

<p>
A variable without a sigil is a local variable. A local variable is valid
only locally. For example inside a method, block or a module. 
</p>

<pre class="explanation">
$car_name = "Peugeot"
</pre>

<p>
Global variables start with $ character. They are valid everywhere. The use
of global variables should be limited in programs. 
</p>

<pre class="explanation">
@sea_name = "Black sea"
</pre>

<p>
A variable name starting with a @ sigil is an instance variable. This
variable is valid inside an object. 
</p>

<pre class="explanation">
@@species = "Cat"
</pre>

<p>
Finally we have a class variable. This variable is valid for all instances
of a specific class. 
</p>

<pre class="explanation">
p local_variables
</pre>

<p>
The <code>local_variables</code> gives an array of all local variables
defined in a specific context. Our context is Ruby toplevel.
</p>

<pre class="explanation">
p global_variables.include? :$car_name
</pre>

<p>
Similarly, the <code>global_variables</code> produces an array of globals.
We do not print all globals to the terminal, because there are many of them.
Each Ruby script starts with a bunch of predefined variables. Instead of that,
we call the <code>include?</code> method of the array to check
if our global is defined in the array. Also note that we are referencing 
variables with their symbols. (Symbols start with a colon character.)
</p>

<pre class="explanation">
p self.instance_variables
</pre>

<p>
The <code>self</code> pseudo variable points to the receiver of the
<code>instance_variables</code> method. The receiver in our case is the
<b>main</b>, the Ruby toplevel execution area. 
</p>

<pre class="explanation">
p Object.class_variables
</pre>

<p>
Finally we have an array of class variables. The <b>main</b> is
an instance of the <code>Object</code> class. 
</p>

<pre>
$ ./sigils.rb
[:tree_name]
true
[:@sea_name]
[:@@species]
</pre>

<p>
Output of the example. We see symbolic names of the variables. 
</p>


<h2>Local variables</h2>

<p>
Local variables are variables that are valid within a local area
of a Ruby source code. This area is also referred to as <b>local scope</b>.
Local variables exist within the definition of a Ruby module, method,
class.
</p>

<pre class="code">
#!/usr/bin/ruby

def method1
   x = 5
   p x    
end

method1 

p x
</pre>

<p>
We have a method called method1, which has one
variable. The variable is local. This means that 
it is valid only within the method definition. 
We can refer to the x variable only between the
method name and the <code>end</code> keyword. 
</p>

<pre class="explanation">
def method1
   x = 5
   p x    
end
</pre>

<p>
This is the definition of the method1 method. Inside the
method, we create a local x variable. We print the value
of the variable to the terminal.
</p>

<pre class="explanation">
method1 
</pre>

<p>
The method is called. 
</p>

<pre class="explanation">
p x
</pre>

<p>
We try to refer to a local variable outside the definition
of the method. This leads to a <code>NameError</code>.
The Ruby interpreter cannot find such identifier. 
</p>

<pre>
$ ./locals.rb
5
./locals.rb:11:in `&lt;main&gt;': undefined local variable 
or method `x' for main:Object (NameError)
</pre>

<p>
Running the example gives the above output.
</p>

<hr class="btm">

<p>
The following example is a slight modification of a previous
example.
</p>

<pre class="code">
#!/usr/bin/ruby

x = 5

def method1
    x = 10
    p x
end

method1

p x
</pre>

<p>
We have two x variables. One is defined inside the method1
and the other one is defined outside. They are two distinct
local variables. They do not clash with each other.
</p>

<pre class="explanation">
x = 5
</pre>

<p>
We have created a local x variable, which holds value 5. The variable
is valid in the local scope of the main execution area. It is not
valid inside the method1. 
</p>

<pre class="explanation">
def method1
    x = 10
    p x
end
</pre>

<p>
Inside the definition of the method1 a new local variable
x is defined. It has value 10. It exists in the body of the method1 method.
After the <code>end</code> keyword it ceases to exist. 
</p>

<pre>
$ ./locals2.rb
10
5
</pre>

<p>
Output.
</p>

<hr class="btm">

<p>
If a method takes parameters, a local variable is created for 
each of these parameters. 
</p>

<pre class="code">
#!/usr/bin/ruby


def rectangle_area a, b
    puts local_variables
    return a * b
end

puts rectangle_area 5, 6
</pre>

<p>
We have a method definition, which takes two values.
The method returns the area of a rectangle. 
</p>

<pre class="explanation">
def rectangle_area a, b
    puts local_variables
    return a * b
end
</pre>

<p>
The rectangle_area method takes two parameters. They
are the sides of a rectangle, for which we calculate
the area. Two local variables are automatically created
for a, b identifiers. We call the <code>local_variables</code>
method to see, what local variables we have in the method. 
</p>

<pre class="explanation">
puts rectangle_area 5, 6
</pre>

<p>
Here we pass two values to the method rectangle_area. 
The values will be assigned to two local variables, created
inside the method. 
</p>

<pre>
$ ./parameters.rb
a
b
30
</pre>

<p>
The output shows three things. The first two are the names of the
local variables within the rectangle_area method. The third is the
calculated area of the given rectangle. 
</p>

<hr class="btm">

<p>
A method may be defined inside another method. The inner methods
have their own local variables. 
</p>

<pre class="code">
#!/usr/bin/ruby

def method1
    
    def method2
                
        def method3
            m5, m6 = 3
            puts "Level 3"
            puts local_variables
        end            
        
        m3, m4 = 3
        puts "Level 2"
        puts local_variables
        method3    
    end        
    
    m1, m2 = 3
    puts "Level 1"
    puts local_variables
    method2
            
end

method1
</pre>

<p>
In this Ruby script, we create three methods. The method2 and method3
are inner methods. The method2 is defined inside the method1 and the
method3 is defined inside method2. The local variables of each of the
method is only accessible in the method, in which they were defined.
</p>

<pre>
$ ./lms.rb
Level 1
m1
m2
Level 2
m3
m4
Level 3
m5
m6
</pre>

<p>
From the output we can see, that method1 has two local variables, m1 and m2.
The inner method2 has m3, m4 local variables. The innermost method, method3
has m5, m6 local variables. 
</p>

<hr class="btm">

<p>
The last example of this section will present
several demonstrations of a local scope. 
</p>

<pre class="code">
module ModuleM
    m1, m2 = 4
    
    puts "Inside module"
    puts local_variables        
end


def method1
    v, w = 3
    puts "Inside method"
    puts local_variables
end


class Some
    x, y = 2
    puts "Inside class"
    puts local_variables
end 

method1

t1, t2 = 7

puts "Inside toplevel"
puts local_variables   
</pre>

<p>
In the code example, we create local variables inside a module,
method, class and toplevel. The <code>local_variables</code>
is a method of the <code>Kernel</code> module, which returns all
current local variables.
</p>

<pre class="explanation">
module ModuleM
    m1, m2 = 4
    
    puts "Inside module"
    puts local_variables        
end
</pre>

<p>
A module is a collection of methods and constants.
We create two local variables m1 and m2.
</p>

<pre class="explanation">
def method1
    v, w = 3
    puts "Inside method"
    puts local_variables
end
</pre>

<p>
Two local variables v, w are created in the method1 method.
</p>

<pre class="explanation">
class Some
    x, y = 2
    puts "Inside class"
    puts local_variables
end 
</pre>

<p>
The x, y local variables are created inside the 
definition of the Some class. 
</p>

<pre class="explanation">
t1, t2 = 7
</pre>

<p>
Finally two local variables that belong to the
local scope of the Ruby toplevel.
</p>

<pre>
$ ./locals3.rb 
Inside module
m1
m2
Inside class
x
y
Inside method
v
w
Inside toplevel
t1
t2
</pre>

<p>
The output shows local variables for each local scope.
</p>


<h2>Global variables</h2>

<p>
Global variables are valid everywhere in the script. They start
with a $ sigil in Ruby. 
</p>

<p>
The use of global variables is discouraged. Global variables easily
lead to many programming errors. Global variables should be used 
only when there is a reason to do so. Instead of global variables, 
programmers are advised to use local variables whenever possible.
</p>

<pre class="code">
#!/usr/bin/ruby

$gb = 6


module ModuleM        
    puts "Inside module"
    puts $gb
end


def method1
    puts "Inside method"
    puts $gb
end


class Some
    puts "Inside class"
    puts $gb
end 

method1

puts "Inside toplevel"
puts $gb
puts global_variables.include? :$gb
</pre>

<p>
In the example we have a global variable $gb. We show
that the variable can be referenced in a module, method, class and
a toplevel. The global variable $gb is valid in all these entities.
</p>

<pre class="explanation">
$gb = 6
</pre>

<p>
A global variable $gb is created; it has value 6.
</p>

<pre class="explanation">
module ModuleM        
    puts "Inside module"
    puts $gb
end
</pre>

<p>
Inside the defition of a module we print the value
of the global variable.
</p>

<pre class="explanation">
def method1
    puts "Inside method"
    puts $gb
end
</pre>

<p>
Inside the defition of a method we print the value
of the global variable.
</p>

<pre class="explanation">
class Some
    puts "Inside class"
    puts $gb
end
</pre>

<p>
Inside the defition of a class we print the value
of the global variable.
</p>

<pre class="explanation">
puts $gb
puts global_variables.include? :$gb
</pre>

<p>
Finally in the toplevel execution area we print the value
of the global variable and , if the variable is
in the array produced by the <code>global_variables</code>
method. 
</p>

<pre>
$ ./globals.rb
Inside module
6
Inside class
6
Inside method
6
Inside toplevel
6
true
</pre>

<p>
The output of the example confirms, that the global variable is accessible
everywhere. 
</p>

<hr class="btm">

<p>
When a Ruby script starts, it has access to multiple predefined global variables. 
These globals are not considered harmful and help solve common programming 
jobs. 
</p>

<pre class="code">
#!/usr/bin/ruby

p $LOAD_PATH
p $:
</pre>

<p>
The script shows a <code>$LOAD_PATH</code> global variable. The
variable lists directories which are searched by <code>load</code>
and <code>require</code> methods. The <code>$:</code> is a short synonym
for the <code>$LOAD_PATH</code> name.
</p>

<p>
More global variables will be presented in the Predefined variables section
of this chapter. 
</p>


<h2>Instance, class variables</h2>

<p>
In this section we will briefly cover instance and class variables.
They will be described in Object-oriented programming chapter in more
detail. 
</p>

<p>
<b>Instance variables</b> are variables that belong to a particular object
instance. Each object has its own object variables. Instance variables start
with a @ sigil. <b>Class variables</b> belong
to a specific class. All objects created from a particular class share class
variables. Class variables start with @@ characters.
</p>


<pre class="code">
#!/usr/bin/ruby


class Being
    
    @@is = true
       
    def initialize nm
        @name = nm
    end
    
    def to_s
        "This is #{@name}"
    end    
    
    def does_exist?
        @@is
    end
end

b1 = Being.new "Being 1"
b2 = Being.new "Being 2"
b3 = Being.new "Being 3"

p b1, b2, b3

p b1.does_exist?
p b2.does_exist?
p b3.does_exist?
</pre>

<p>
We create a custom Being class. The Being class has one class and
one instance variable. 
</p>

<pre class="explanation">
class Being
    
    @@is = true
</pre>

<p>
The @@is is an class variable. This variable is shared by all
instances of the Being class. The logic of this example is that
Being is and NotBeing is not. 
</p>

<pre class="explanation">
def initialize nm
    @name = nm
end
</pre>

<p>
The <code>initialize</code> method is a constructor. The method
is called when the object is created. A @name instance variable is
created. This variable is specific to a concrete object. 
</p>

<pre class="explanation">
def to_s
    "This is #{@name}"
end       
</pre>

<p>
The <code>to_s</code> method is called, when the object is a parameter
of a printing method, like <code>p</code> or <code>puts</code>. In our
case, the method gives a short human readable description of the object. 
</p>

<pre class="explanation">
def does_exist?
    @@is
end
</pre>

<p>
The does_exist? method returns the class variable. 
</p>

<pre class="explanation">
b1 = Being.new "Being 1"
b2 = Being.new "Being 2"
b3 = Being.new "Being 3"
</pre>

<p>
Three objects from the Being class are created. Each of the
objects has a different name. The name of the object will
be stored in the instance method, which is unique to each object
instance. This will be used in the <code>to_s</code> method, which
give a short description of the object. 
</p>

<pre class="explanation">
p b1, b2, b3
</pre>

<p>
The <code>p</code> method takes the created objects as three parameters. 
It calls the <code>to_s</code> method on each of these objects.
</p>

<pre class="explanation">
p b1.does_exist?
p b2.does_exist?
p b3.does_exist?
</pre>

<p>
Finally, we call the does_exist? method of each of the instances and
print their return values. The output of these three methods is the
same, because each method returns the class variable. 
</p>

<pre>
$ ./icvars.rb
This is Being 1
This is Being 2
This is Being 3
true
true
true
</pre>

<p>
Output of the example. The first three messages are unique. The
strings are stored in the instance variables of the objects. 
The true value is the value of the class variable, which is
called three times. 
</p>


<h2>Environment &amp; command-line variables</h2>

<p>
The <code>ENV</code> constant gives access to environment variables. 
It is a Ruby hash. Each environment variable is a key to the 
<code>ENV</code> hash. 
</p>

<p>
The <code>ARGV</code> constant holds command-line argument values. 
They are passed by the programmer when the script is launched. 
The <code>ARGV</code> is an array which stores the arguments as
strings. The <code>$*</code> is an alias to the <code>ARGV</code>.
</p>

<p>
Both <code>ENV</code> and <code>ARGV</code> are global constants.
</p>


<pre class="code">
#!/usr/bin/ruby

ARGV.each do |a|
  puts "Argument: #{a}"
end
</pre>

<p>
In the script we loop through the <code>ARGV</code> array and 
print each of its values.
</p>


<pre>
$ ./commandline.rb 1 2 3
Argument: 1
Argument: 2
Argument: 3
</pre>

<p>
We have given three command-line arguments. They are printed
to the console. Each on a separate line. 
</p>

<hr class="btm">

<p>
The following example will deal with environment variables. 
</p>

<pre class="code">
#!/usr/bin/ruby

puts ENV['SHELL']
puts ENV['LANG']
puts ENV['TERM']
</pre>

<p>
The script will print values of three environment variables to the
terminal. The values depend on the OS settings of our operating system. 
</p>

<pre>
$ ./environment.rb
/bin/bash
en_US.utf8
xterm
</pre>

<p>
A sample output.  
</p>


<h2>Pseudo variables</h2>

<p>
Ruby has a few variables which are called <b>pseudo variables</b>. They
are different from regular variables. We cannot assign values to pseudo variables.
</p>

<p>
The <code>self</code> is the receiver of the current method. 
The <code>nil</code> is the sole instance of the <code>NilClass</code>.
It represents the absense of a value. The <code>true</code> is the
sole instance of the <code>TrueClass</code>. It represents boolean true. 
The <code>false</code> is a sole instance of <code>FalseClass</code>. 
It represents boolean false. 
</p>

<p>
The true and false are values of a boolean datatype. From another point
of view, they are instances of specific classes. This is because everything
in Ruby is an object. This looks like unnecessarily complicated. 
But it is the consequence of the aforementioned Ruby idiom. 
</p>

<pre class="code">
#!/usr/bin/ruby

p self
p nil
p true
p false

p self.class
p nil.class
p true.class
p false.class
</pre>

<p>
This is an example of pseudo variables. We print all four
pseudo variables with the <code>p</code> method. Then we
find out the class name for all of them. 
</p>

<pre class="explanation">
p self
</pre>

<p>
In this context, the <code>self</code> pseudo variable returns
the main execution context. 
</p>

<pre>
$ ./pseudo.rb
main
nil
true
false
Object
NilClass
TrueClass
FalseClass
</pre>

<p>
Example output. 
</p>

<hr class="btm">

<p>
In the second example of this section, we will further look
at the <code>self</code>.
</p>

<pre class="code">
#!/usr/bin/ruby

class Some
    puts self
end

class Other
    puts self
end

puts self
</pre>

<p>
As we have said, the <code>self</code> gives the
receiver of the current method. The above example shows
three examples of different receivers. 
</p>

<pre class="explanation">
class Some
    puts self
end
</pre>

<p>
The receiver is the class called Some. 
</p>

<pre class="explanation">
class Other
    puts self
end
</pre>

<p>
Here is another receiver. A class named Other.
</p>

<pre class="explanation">
puts self
</pre>

<p>
And the third receiver is the Ruby toplevel.
</p>

<pre>
$ ./pseudoself.rb
Some
Other
main
</pre>

<p>
Example output.
</p>

<hr class="btm">

<p>
The last example of the section will
present other three pseudo variables. 
</p>

<pre class="code">
#!/usr/bin/ruby

if true
    puts "This message is shown"
end

if false
    puts "This message is not shown"
end

p $name
p $age
</pre>

<p>
The above example shows <code>true</code>, <code>false</code>
and <code>nil</code> pseudo variables at work. 
</p>

<pre class="explanation">
if true
    puts "This message is shown"
end
</pre>

<p>
The <code>true</code> is used in boolean expression. 
The message is always printed. 
</p>

<pre class="explanation">
if false
    puts "This message is not shown"
end
</pre>

<p>
This message is never printed. The condition 
is not met. In the boolean expression we always
get a negative value. 
</p>

<pre class="explanation">
p $name
p $age
</pre>

<p>
If global values are referenced and have not been
initialized, they contain the <code>nil</code> pseudo variable.
It stands for the absence of a value. 
</p>

<pre>
$ ./pseudo2.rb
This message is shown
nil
nil
</pre>

<p>
Output of the pseudo2.rb Ruby script. 
</p>


<h2>Predefined variables</h2>

<p>
Ruby has plenty of predefined global variables. This is a heritage of Perl 
language. Ruby was influenced strongly by Perl. They are at disposal when 
the Ruby script starts. We have a few examples for the predefined Ruby variables. 
</p>

<pre class="code">
#!/usr/bin/ruby

print "Script name: ", $0, "\n"
print "Command line arguments: ", $*, "\n"

puts "Process number of this script: #{$$}"
</pre>

<p>
Three predefined variables have been used. $0, $* and
$$. The $0 stores the current script name. The $* variable
stores command-line arguments. And the $$ stores the PID (process id) of
the script. 
</p>

<pre>
$ ./predefined.rb 1 2 3
Script name: ./predefined.rb
Command line arguments: ["1", "2", "3"]
Process number of this script: 3122
</pre>

<p>
Sample output.
</p>


<hr class="btm">

<p>
The $? global variable stores the exit status of 
the last executed child process.
</p>

<pre class="code">
#!/usr/bin/ruby

system 'echo "Ruby"'
puts $?

%x[exit '1']
puts $?
</pre>

<p>
We run two external child processes and check their
exit status with the $? variable. 
</p>

<pre class="explanation">
system 'echo "Ruby"'
puts $?
</pre>

<p>
With the use of the <code>system</code> method we start a
child process. It is an echo bash command, which prints a message
to the terminal. 
</p>

<pre class="explanation">
%x[exit '1']
puts $?
</pre>

<p>
In the second case we execute exit bash command with status 1. 
</p>

<pre>
$ ./predefined2.rb
Ruby
pid 3131 exit 0
pid 3133 exit 1
</pre>

<p>
The first child process terminates with status 0, the second with
exit status 1. 
</p>

<hr class="btm">

<p>
The $; variable has the default separator of the <code>split</code> method
of the <code>String</code> class. 
</p>

<pre class="code">
#!/usr/bin/ruby

str = "1,2,3,4,5,6,7"
p str.split

$; = ","
p str.split
</pre>

<p>
We use the $; variable to control, how the string is cut with the 
<code>split</code> method. The method takes a parameter, which tells
where the string should be split. If the parameter is omitted, than 
the value from the $; is used. 
</p>

<pre class="explanation">
$; = ","
p str.split
</pre>

<p>
We specify a delimiter for the $; variable. The split method does not
take a parameter, so the value of the $; is used. 
</p>

<pre>
$ ./predefined3.rb
["1,2,3,4,5,6,7"]
["1", "2", "3", "4", "5", "6", "7"]
</pre>

<p>
In the first case, the string was not split. In the second case,
the string was split correctly, as we have intended. 
</p>

<hr class="btm">

<p>
In the final example, we show three global predefined variables which
are used with regular expressions. 
</p>

<pre class="code">
#!/usr/bin/ruby

"Her name is Jane" =~ /name/

p $`
p $&amp;
p $'
</pre>

<p>
When we apply the =~ operator on a string, Ruby sets some variables. 
The $&amp; variable has a string that matched the last last regular
expression match. The $` has a string preceding $&amp; and the $’ 
has a string following the $&amp;.
</p>

<pre>
$ ./predefined4.rb
"Her "
"name"
" is Jane"
</pre>

<p>
Example output.
</p>

<p>
In this part of the Ruby tutorial, we looked deeper at the
Ruby variables. 
</p>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified December 14, 2011  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body>
</html>
